//
//  DSZSearchViewController.m
//  7.13苏宁框架
//
//  Created by app01 on 15/7/20.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZSearchViewController.h"
#import "DSZTopicViewController.h"
#import "TopicCellModel.h"
#import "DSZContentViewController.h"
#import "SearchCell.h"
#import "Common.h"

@interface DSZSearchViewController () <CareClickDelegate,UIGestureRecognizerDelegate>


@property(weak,nonatomic)UIView *cover;

-(void)loadData;



@end

@implementation DSZSearchViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    _care = [[defaults objectForKey:@"care"] integerValue];
    
    
    _tap.delegate = self;
    _tableView.hidden = YES;
    _searchItem.hidden = YES;
    _searchBG.layer.cornerRadius = 3;
    _searchBG.layer.masksToBounds = YES;
    
    UIView *cover = [[UIView alloc]initWithFrame:self.view.bounds];
    cover.backgroundColor = [UIColor grayColor];
    [self.view insertSubview:cover belowSubview:_searchBar];
    _cover = cover;
    
}


//-(void)viewDidDisappear:(BOOL)animated{
//    [self.navigationController popViewControllerAnimated:NO];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)searchItemAction:(id)sender {
    _cover.hidden = NO;
    _searchBar.hidden = NO;
    [_searchBar resignFirstResponder];
    _searchItem.hidden = YES;
    _searchBG.hidden = NO;
    _search.hidden = NO;
    _tableView.hidden = YES;
    
}

- (IBAction)searchAction:(id)sender {
    
    [self.searchBar resignFirstResponder];
    _cover.hidden = YES;
    _searchBar.hidden = YES;
    _searchItem.hidden = NO;
    _searchBG.hidden = YES;
    _search.hidden = YES;
    
    
    [self loadData];
    [_tableView reloadData];
    
    self.searchBar.text = @"";
    

}

- (IBAction)tapScreen:(id)sender {
  
    _cover.hidden = YES;
    _searchBar.hidden = YES;
    _searchItem.hidden = NO;
    _searchBG.hidden = YES;
    _search.hidden = YES;
    _tableView.hidden = YES;

}

//控制点击事件优先还是点击单元格的事件优先
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([touch.view isMemberOfClass:[UIView class]])
    {
        return YES;
    }else{
        return NO;
    } 
}





-(void)loadData{
    _resultArray = [NSMutableArray array];
    for (TopicCellModel *model in _array) {
        NSString *string = model.title;
        NSString *string1 = model.descrip;
        if ([string containsString:_searchBar.text] | [string1 containsString:_searchBar.text]) {
            [_resultArray addObject:model];
        }else{
            _tableView.hidden = YES;
        }
    }

}




#pragma mark  CareClickDelegate代理方法


-(void)clickCareBtn:(UIButton *)btn model:(TopicCellModel *)model{
    
    if (btn.tag == 1) {
        model.iscare = NO;
        _care--;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@(_care) forKey:@"care"];
        btn.layer.borderColor = [UIColor orangeColor].CGColor;
        btn.selected=NO;
        btn.tag = 0;
    }else{
        model.iscare = YES;
        _care++;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@(_care) forKey:@"care"];
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        btn.selected=YES;
        btn.tag = 1;

    }
  
    
}

#pragma mark SearchBar代理方法

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar resignFirstResponder];
    [self searchAction:nil];
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{
    
}

#pragma mark tableview代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_resultArray.count>1) {
        _tableView.hidden = NO;
    }
    return _resultArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"cell";
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[SearchCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.model = _resultArray[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.delegate = self;
    

    return cell;
    
}

//选中表格事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DSZContentViewController *contentVC = [[DSZContentViewController alloc]init];
    TopicCellModel *cellmodel =_resultArray[indexPath.row];
    
    contentVC.headerModel = (TopicModel *)cellmodel;
    contentVC.cotentCollectionArray = cellmodel.contents;
    [self.navigationController pushViewController:contentVC animated:YES];
}



-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"话题";
}

@end
